package com.example.rabbitmqdemo.controller;

import com.example.rabbitmqdemo.config.RabbitTemplateConfig;
import com.example.rabbitmqdemo.constant.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.annotation.RequestScope;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-04-03
 * Time: 9:23
 */
@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Resource(name = "rabbitTemplate")
    private RabbitTemplate rabbitTemplate;

    @Resource(name = "confirmRabbitTemplate")
    private RabbitTemplate rabbitTemplateConfig;

    @RequestMapping("/ack")
    public String ack() {
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE,"ack","ack is ok");
        return "ack is ok!";
    }

    @RequestMapping("/pres")
    public String pres() {
        Message message  = new Message("Presistent test...".getBytes(),new MessageProperties());
        //消息持久化
        //message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        //消息非持久化
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        //System.out.println(message);
        rabbitTemplate.convertAndSend(Constants.PRES_EXCHANGE,"pres",message);
        return "pres is ok!";
    }

    @RequestMapping("/confirm")
    public String confirm() {

        CorrelationData correlationData = new CorrelationData("1");
        rabbitTemplateConfig.convertAndSend(Constants.CONFIRM_EXCHANGE,"confirm","confirm test...",correlationData);
        return "confirm is ok!";
    }

    @RequestMapping("/returns")
    public String returns() {

        CorrelationData correlationData = new CorrelationData("5");
        rabbitTemplateConfig.convertAndSend(Constants.CONFIRM_EXCHANGE,"confirm1","confirm test...",correlationData);
        return "returns is ok!";
    }

    @RequestMapping("/retry")
    public String retry() {
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE,"retry","retry test...");
        return "retry is ok!";
    }

    @RequestMapping("/ttl")
    public String ttl() {
        //设置过期时间
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("20000");//毫秒
                return message;
            }
        };
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","ttl test...",messagePostProcessor);
        MessagePostProcessor messagePostProcessor2 = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000");//毫秒
                return message;
            }
        };
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","ttl test...",messagePostProcessor2);
        return "ttl is ok!";
    }

    @RequestMapping("/delay")
    public String delay() {
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("30000");
                return message;
            }
        };
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal","delay test...",messagePostProcessor);

        MessagePostProcessor messagePostProcessor1 = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000");
                return message;
            }
        };
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal","delay test...",messagePostProcessor);

        return "delay is ok!";
    }

    @RequestMapping("/ttl2")
    public String ttl2() {
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","ttl2 test...");
        return "ttl2 is ok!";
    }

    @RequestMapping("/dl1")
    public String dl1() {
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal","normal test...");
        return "dl test!";
    }

    @RequestMapping("/dl")
    public String dl() {
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal","normal test... " + i);
        }
        return "dl test!";
    }

    @RequestMapping("/delay2")
    public String delay2() {
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelayLong(30000L);
                return message;
            }
        };
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,"delay","delay test is running...",messagePostProcessor);

        MessagePostProcessor messagePostProcessor2 = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelayLong(10000L);
                return message;
            }
        };
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,"delay","delay test is running...",messagePostProcessor2);
        System.out.printf("%tc 消息发送成功 \n",new Date());

        return "delay2 is ok!";
    }

    @RequestMapping("/trans")
    public String trans() {
        System.out.println("事务测试");
        //使用内置交换机
        rabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans test1....");
        int sum = 3 / 0;
        rabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans test2....");
        return "trans is ok!";
    }

    @Resource(name = "transRabbitTemplate")
    private RabbitTemplate transRabbitTemplate;
    @Transactional
    @RequestMapping("/trans2")
    public String trans2() {
        System.out.println("事务测试");
        //使用内置交换机
        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans test1....");
        int sum = 3 / 0;
        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans test2....");
        return "trans is ok!";
    }


    @RequestMapping("/qos")
    public String qos() {
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend(Constants.QOS_EXCHANGE,"qos","qos test...." + i);
        }
        return "trans is ok!";
    }

    @RequestMapping("/quorum")
    public String quorum() {
        rabbitTemplate.convertAndSend("","quorum.queue","quorum test....");
        return "quorum is ok!";
    }


    @RequestMapping("/cluster")
    public String cluster() {
        rabbitTemplate.convertAndSend("","cluster.queue","cluster test....");
        return "cluster is ok!";
    }


}
